package com.yunchuang.service;

import com.yunchuang.constants.FileStatusConstants;
import com.yunchuang.pojo.FileInfo;
import com.yunchuang.utils.ProfileUtils;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 接收请求
 *
 * @author ItJiange
 * @date 2023/10/19
 */
public class ReceiveService {
    public void receiveService() {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(ProfileUtils.getPort());
        } catch (IOException e) {
            e.printStackTrace();
        }

        while (true) {
            try (Socket socket = serverSocket.accept()) {
                if (socket == null) {
                    System.out.println("连接失败！");
                    return;
                }
                System.out.print("连接客户端 IP 地址：" + socket.getLocalAddress() + " ");

                // 获取 FileInfo 对象
                FileInfo fileInfo = getFileInfo(socket);
                // 根据文件状态响应对应的操作
                operateFileByStatus(fileInfo);

                Thread.sleep(1000);
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 操作文件
     *
     * @param fileInfo
     */
    private void operateFileByStatus(FileInfo fileInfo) {
        String fileName = fileInfo.getFileName();
        String fileStatus = fileInfo.getFileStatus();
        byte[] fileMessage = fileInfo.getFileMessage();

        if (!FileStatusConstants.FILE_DELETE.equals(fileStatus)) {
            writeFileMessage(fileName, fileMessage);
            System.out.println(fileStatus + "成功！ " + fileStatus + " : " + fileName);
        } else {
            deleteFile(fileName);
        }
    }

    /**
     * 删除文件
     * @param fileName
     */
    private static void deleteFile(String fileName) {
        String context = ProfileUtils.getContext();
        File file = new File(context + File.separator + fileName);
        if (file.delete()) {
            System.out.println("删除文件成功，文件名 : " + fileName);
        } else {
            System.out.println("删除失败");
        }
    }

    /**
     * 写入文件内容
     * @param fileName
     * @param fileMessage
     */
    private static void writeFileMessage(String fileName, byte[] fileMessage) {
        String context = ProfileUtils.getContext();
        try (FileOutputStream fos = new FileOutputStream(context + File.separator + fileName);) {
            fos.write(fileMessage);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取文件对象
     *
     * @param socket
     * @return
     */
    private FileInfo getFileInfo(Socket socket) {
        // 获取 socket 输入流
        try (InputStream socketInput = socket.getInputStream();
             ObjectInputStream fileInfoInput = new ObjectInputStream(socketInput)) {
            // 反序列化获取文件对象
            return (FileInfo) fileInfoInput.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
}
